package com.gm.wholesale.module.custom.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gm.commonlib.base.baseBean.BaseResp;
import com.gm.commonlib.base.baseBean.DataResp;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.base.baseui.baseFragment.BaseFragment;
import com.gm.commonlib.interfaces.ICallBackListener;
import com.gm.commonlib.interfaces.IResultListener;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.views.popupwindow.Popupwindow;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.BuildConfig;
import com.gm.wholesale.R;
import com.gm.wholesale.module.config.ConfigData;
import com.gm.wholesale.module.custom.bean.CustomBean;
import com.gm.wholesale.module.custom.model.CustomModel;
import com.gm.wholesale.module.good.bean.GoodListBean;
import com.gm.wholesale.module.good.model.GoodModel;
import com.gm.wholesale.module.user.UserDataManger;
import com.gm.wholesale.module.wx.bean.ShareHtmlBean;
import com.gm.wholesale.module.wx.share.WXShareUtil;

public class CustormFunctionView extends LinearLayout {
    private LinearLayout deleteGoodView;

    private LinearLayout inviteView;

    private Context mContext;

    public CustormFunctionView(Context context) {
        super(context);
        initView(context);
    }

    public CustormFunctionView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.view_custorm_function, this);
        deleteGoodView = findViewById(R.id.deleteGoodView);
        inviteView = findViewById(R.id.inviteView);

    }

    public void showView(CustomBean customBean, IResultListener<CustomBean> iCallBackListener) {
        if (customBean == null) return;
        setVisibility(VISIBLE);
        //有权限才有邀请按钮
        if (ConfigData.getConfigData().hasNeedOrderPermission()){
            inviteView.setVisibility(VISIBLE);
        }
        else {
            inviteView.setVisibility(GONE);
        }
        deleteGoodView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                CustomModel.deleteCustorm((IBaseView) mContext, customBean.getId(),customBean.getVersion(), new BaseHttpObserver<BaseResp>() {
                    @Override
                    protected void handleResultOkResp(BaseResp resp) {
                        iCallBackListener.onResult(customBean);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        iCallBackListener.onResult(null);
                    }
                });
            }
        });
        inviteView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                iCallBackListener.onResult(null);
                CustomModel.getInviteOrder((IBaseView) mContext, customBean.getId(), new BaseHttpObserver<DataResp<String>>() {
                    @Override
                    protected void handleResultOkResp(DataResp<String> resp) {
                        showInviteView(customBean,resp.getData());
                    }
                });

            }
        });
    }

    private void showInviteView(CustomBean customBean,String token){
        ShareView shareView=new ShareView(getContext());
        ShareHtmlBean shareHtmlBean=new ShareHtmlBean();
        shareHtmlBean.setTitle("向客户\""+customBean.getName()+"\"发起下单邀请");
        String url= BuildConfig.SHARE_HOST+"?token="+token;
        shareHtmlBean.setHtmlUrl(url);
        shareView.show(shareHtmlBean, new OnClickListener() {
            @Override
            public void onClick(View v) {
                Popupwindow.hiddenPop();
                shareHtmlBean.setTitle("邀请您下单");
                shareHtmlBean.setContent(UserDataManger.newInstance().getUserName()+"邀请您下单，快来下单吧");
                WXShareUtil.sendHtml(mContext,shareHtmlBean);
            }
        });
        Popupwindow.showPopCenterView((Activity) mContext,shareView);
    }

    public void hidView() {
        setVisibility(GONE);
    }
}
